//
//  WelcomeViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/29.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "WelcomeViewController.h"
#import "AppDelegate.h"

#define WELCOME_PIC_COUND (3)

@interface WelcomeViewController ()<UIScrollViewDelegate> {
    UIScrollView                    *scrollView;
    UIPageControl                   *pageControl;
}

@end

@implementation WelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self onCreateUI];
}

- (void)onCreateUI
{
    scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.pagingEnabled = YES;
//    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*WELCOME_PIC_COUND, SCREEN_HEIGHT);
    scrollView.userInteractionEnabled = YES;
    [self.view addSubview:scrollView];
    
    for (int i=0; i<WELCOME_PIC_COUND; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, scrollView.width, scrollView.height)];
        imgView.tag = 50+i;
        imgView.backgroundColor = [UIColor whiteColor];
        imgView.userInteractionEnabled = YES;
        [scrollView addSubview:imgView];
        if (i==0) {
            imgView.image = [UIImage imageNamed:@"welcom_1"];
            if (ISIPAD) {
                 imgView.image = [UIImage imageNamed:@"yd1"];
            }
        }
        else if (i==1) {
            
            imgView.image = [UIImage imageNamed:@"welcom_2"];
            if (ISIPAD) {
                imgView.image = [UIImage imageNamed:@"yd2"];
            }
        }
        else if (i==2) {
            imgView.image = [UIImage imageNamed:@"welcom_3"];
            if (ISIPAD) {
                imgView.image = [UIImage imageNamed:@"yd3"];
            }
            
            UIButton *userBt = [UIButton buttonWithType:UIButtonTypeCustom];
            userBt.tag = 60;
            userBt.backgroundColor = [UIColor clearColor];
            userBt.frame = CGRectMake(imgView.left+(SCREEN_WIDTH-120)/2., SCREEN_HEIGHT-30-64-40, 120, 64);
//            [userBt setTitle:@"马上进入" forState:UIControlStateNormal];
            [userBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            userBt.titleLabel.font = [UIFont systemFontOfSize:13.f];
//            [userBt setBackgroundImage:[UIImage imageNamed:@"bofangquanbu2"] forState:UIControlStateNormal];
//            [userBt setBackgroundImage:[UIImage imageNamed:@"bofangquanbu"] forState:UIControlStateSelected];
            [userBt addTarget:self action:@selector(onSelectForUserAction) forControlEvents:UIControlEventTouchUpInside];
//            [scrollView addSubview:userBt];
            
        }
        
    }
    
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-WELCOME_PIC_COUND*10.0f-40.f)/2.,SCREEN_HEIGHT - 30, WELCOME_PIC_COUND*10.0f+40.f, 20)];
    pageControl.currentPage=0;
    pageControl.numberOfPages= WELCOME_PIC_COUND;
    pageControl.hidden = YES;
    [self.view addSubview:pageControl];
    [self.view insertSubview:pageControl aboveSubview:scrollView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*WELCOME_PIC_COUND, SCREEN_HEIGHT);
    for (int i=0; i<WELCOME_PIC_COUND; i++) {
        UIImageView *imgView = (UIImageView *)[scrollView viewWithTag:50+i];
        imgView.frame = CGRectMake(SCREEN_WIDTH*i, 0, scrollView.width, scrollView.height);
        if (i==2) {
            UIButton *userBt = (UIButton *)[scrollView viewWithTag:60];
            userBt.frame = CGRectMake(imgView.left+(SCREEN_WIDTH-120)/2., SCREEN_HEIGHT-30-34-20, 120, 34);
        }
    }
    pageControl.frame = CGRectMake((SCREEN_WIDTH-WELCOME_PIC_COUND*10.0f-40.f)/2.,SCREEN_HEIGHT - 30, WELCOME_PIC_COUND*10.0f+40.f, 20);
}


- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (void)onSelectForUserAction
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate setUpTabbar:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = (int)(scrollView.contentOffset.x / scrollView.width);
    if (index < 0) {
        index = 0  ;
    }
    if (index >= WELCOME_PIC_COUND) {
        index = WELCOME_PIC_COUND-1;
    }
    pageControl.currentPage = index;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index = (int)(scrollView.contentOffset.x / scrollView.width);
    if (index == 2) {
        float offset = (scrollView.contentOffset.x - scrollView.width*index);
        if (offset >= 30) {
            [self onSelectForUserAction];
        }
    }
}

@end
